package net.zjitc.controller;

import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import net.zjitc.common.BaseResponse;
import net.zjitc.common.ErrorCode;
import net.zjitc.common.ResultUtils;
import net.zjitc.exception.BusinessException;
import net.zjitc.model.domain.User;
import net.zjitc.model.vo.CountDataVO;
import net.zjitc.model.vo.CountLikeAndCommentVO;
import net.zjitc.model.vo.CountWeekVO;
import net.zjitc.model.vo.NewNumVO;
import net.zjitc.service.HomeService;
import net.zjitc.service.UserService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.HashMap;

@RestController
@RequestMapping("/home")
@Api(tags = "首页模块")
@Log4j2
public class HomeController {

    @Resource
    private UserService userService;

    @Resource
    private HomeService homeService;

    @GetMapping("/countNew")
    public BaseResponse<NewNumVO> Num(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
            HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        NewNumVO newUserNumVO = homeService.count(date);
        return ResultUtils.success(newUserNumVO);
    }

    @GetMapping("/countData")
    public BaseResponse<CountDataVO> countData(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
            HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        CountDataVO countDataVO = homeService.countData(date);
        return ResultUtils.success(countDataVO);
    }

    @GetMapping("/countWeek")
    public BaseResponse<CountWeekVO> countWeek(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
            HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        CountWeekVO countWeek = homeService.countWeek(date);
        return ResultUtils.success(countWeek);
    }

    @GetMapping("/countLikeAndComment")
    public BaseResponse<CountLikeAndCommentVO> countLikeAndComment(
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date,
            HttpServletRequest request){
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        CountLikeAndCommentVO countLikeAndCommentVO = homeService.countLikeAndComment(date);
        return ResultUtils.success(countLikeAndCommentVO);
    }



}
